import {
  adminUserlist,
  adminUserAdd,
  adminRolelist,
  adminRoleadd,
  adminRoledel,
  adminRoleremove,
  adminUserRemove,
  adminUserDel,
  adminPerList,
  adminPerRemove,
  adminPerDel,
  adminPerAdd,
} from '@/services/admin';
import { message } from 'antd';
export default {
  namespace: 'admin',

  state: {
    list: [],
    pagination: [],
    roles_data: [],
    per_data: [],
    url_data: [],
  },

  effects: {
    *fetchAdminUserList({ payload }, { call, put }) {
      const response = yield call(adminUserlist, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'adminuserlist',
          payload: response.data,
        });
      }
    },
    *AdminUserAdd({ payload }, { call, put }) {
      const response = yield call(adminUserAdd, payload);
      if (response && response.status === 'success') {
        message.success('添加成功');
        const tworesponse = yield call(adminUserlist, payload);
        if (tworesponse && tworesponse.status === 'success') {
          yield put({
            type: 'adminuserlist',
            payload: tworesponse.data,
          });
        }
      }
    },
    *AdminUserUpdate({ payload }, { call, put }) {
      const response = yield call(adminUserRemove, payload);
      if (response && response.status === 'success') {
        message.success('修改成功');
        yield put({
          type: 'adminRolesdel',
          ...payload,
        });
      }
    },
    *AdminUserDel({ payload }, { call, put }) {
      const response = yield call(adminUserDel, payload);
      if (response && response.status === 'success') {
        message.success('删除成功');
        yield put({
          type: 'adminUserdel',
          ...payload,
        });
      }
    },
    *AdminRoleList({ payload }, { call, put }) {
      const response = yield call(adminRolelist, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'adminrolelist',
          payload: response.data,
        });
      }
    },
    *AdminRolesAdd({ payload }, { call, put }) {
      const response = yield call(adminRoleadd, payload);
      if (response && response.status === 'success') {
        message.success('添加成功');
        const response_two = yield call(adminRolelist, payload);
        if (response_two && response_two.status === 'success') {
          yield put({
            type: 'adminrolelist',
            payload: response_two.data,
          });
        }
      }
    },
    *AdminRolesDel({ payload }, { call, put }) {
      const response = yield call(adminRoledel, payload);
      if (response && response.status === 'success') {
        message.success('删除成功');
        yield put({
          type: 'adminRolesdel',
          ...payload,
        });
      }
    },
    *AdminRolesRemove({ payload }, { call, put }) {
      const response = yield call(adminRoleremove, payload);
      if (response && response.status === 'success') {
        message.success('修改成功');
        yield put({
          type: 'adminRolesremove',
          ...payload,
        });
      }
    },
    *AdminPerList({ payload }, { call, put }) {
      const response = yield call(adminPerList, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'adminperlist',
          payload: response.data,
        });
      }
    },
    *AdminPerRemove({ payload }, { call, put }) {
      const response = yield call(adminPerRemove, payload);
      if (response && response.status === 'success') {
        message.success('修改成功');
        yield put({
          type: 'adminPerUpdate',
          ...payload,
        });
      }
    },
    *AdminPerDel({ payload }, { call, put }) {
      const response = yield call(adminPerDel, payload);
      if (response && response.status === 'success') {
        message.success('删除成功');
        yield put({
          type: 'adminRolesdel',
          ...payload,
        });
      }
    },
    *AdminPerAdd({ payload }, { call, put }) {
      const response = yield call(adminPerAdd, payload);
      if (response && response.status === 'success') {
        message.success('添加成功');
        const response_two = yield call(AdminPerList, payload);
        if (response_two && response_two.status === 'success') {
          yield put({
            type: 'adminperlist',
            payload: response_two.data,
          });
        }
      }
    },
  },
  reducers: {
    adminuserlist(state, action) {
      return {
        ...state,
        list: action.payload,
        pagination: action.payload.pagination,
        roles_data: action.payload.roles_data,
        per_data: action.payload.per_data,
      };
    },
    adminrolelist(state, action) {
      return {
        ...state,
        list: action.payload,
        roles_data: action.payload.tree_data,
      };
    },

    adminRolesdel(state, action) {
      for (let i = 0; i < state.list.list.length; i++) {
        if (state.list.list[i].key === action.key) {
          state.list.list.splice(i, 1);
        }
      }
      return {
        ...state,
      };
    },
    adminRolesremove(state, action) {
      for (let i = 0; i < state.list.list.length; i++) {
        if (state.list.list[i].key === action.key) {
          state.list.list[i] = action;
        }
      }
      return {
        ...state,
      };
    },
    adminperlist(state, action) {
      return {
        ...state,
        list: action.payload,
        url_data: action.payload.url_data,
        per_data: action.payload.per_data,
      };
    },
    adminPerUpdate(state, action) {
      for (let i = 0; i < state.list.list.length; i++) {
        if (state.list.list[i].key === action.key) {
          state.list.list[i].pid = action.pid;
          state.list.list[i].rule = action.uid;
          state.list.list[i].title = action.title;
        }
      }
      return {
        ...state,
      };
    },
  },
};
